# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=shaderc
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2020.0
pkgrel=1
pkgdesc="Collection of tools, libraries and tests for shader compilation (mingw-w64)"
url="https://github.com/google/shaderc"
arch=('any')
license=('Apache')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glslang"
         "${MINGW_PACKAGE_PREFIX}-spirv-tools")
makedepends=("${MINGW_PACKAGE_PREFIX}-asciidoctor"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python"
             "${MINGW_PACKAGE_PREFIX}-spirv-headers")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/google/shaderc/archive/v${pkgver}.tar.gz"
        "0001-fix-glslang-hlsl-linking-order.patch")
sha512sums=('1971673d7a5fe77ef827c29db14112d4935c573c7c4f3b00fff3729334284d11640ee40c9e97ebcb2a14c6d5c6db0cf5e2ef6b2dfb59c759d1322c6d38624eb4'
            'eb350ea32e55ee13eae58df8fc6f038503251672134a1dbb131b04a66475595702b4750ae73ecec30afd2dbff67aca746901ff485c420d4a7e474fcbe56ec830')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  # https://github.com/google/shaderc/pull/463
  patch -Nbp1 -i "${srcdir}/0001-fix-glslang-hlsl-linking-order.patch"

  # de-vendor libs and disable git versioning
  sed '/examples/d;/third_party/d' -i CMakeLists.txt
  sed '/build-version/d' -i glslc/CMakeLists.txt
  cat <<- EOF > glslc/src/build-version.inc
"${pkgver}\\n"
"$(pacman -Q "${MINGW_PACKAGE_PREFIX}-spirv-tools"|cut -d \  -f 2|sed 's/-.*//')\\n"
"$(pacman -Q "${MINGW_PACKAGE_PREFIX}-glslang"|cut -d \  -f 2|sed 's/-.*//')\\n"
EOF
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  local -a _extra_config

  if check_option "debug" "y"; then
    _extra_config+=(-DCMAKE_BUILD_TYPE=Debug)
  else
    _extra_config+=(-DCMAKE_BUILD_TYPE=Release)
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G "Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${_extra_config[@]} \
    -DSHADERC_SKIP_TESTS=ON \
    -DSHADERC_ENABLE_WERROR_COMPILE=OFF \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  # Generate manpage
  # Suppress asciidoctor: ERROR: line 1: non-conforming manpage title
  cd "${srcdir}/${_realname}-${pkgver}/glslc"
  ${MINGW_PREFIX}/bin/asciidoctor -b manpage README.asciidoc -o glslc.1 || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/glslc/glslc.1" \
  -t "${pkgdir}${MINGW_PREFIX}/share/man/man1"
}
